<template>
  <el-card class="password-card">
    <h2 class="title">🔐 管理员密码修改</h2>

    <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="top"
    >
      <el-form-item label="原密码" prop="oldPassword">
        <el-input
            v-model="form.oldPassword"
            type="password"
            show-password
            placeholder="请输入当前密码"
            clearable
            :prefix-icon="Lock"
        />
      </el-form-item>

      <el-form-item label="新密码" prop="newPassword">
        <el-input
            v-model="form.newPassword"
            type="password"
            show-password
            placeholder="至少6位且包含字母数字"
            clearable
            :prefix-icon="Lock"
        />
        <password-strength :password="form.newPassword"/>
      </el-form-item>

      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
            v-model="form.confirmPassword"
            type="password"
            show-password
            placeholder="请再次输入新密码"
            clearable
            :prefix-icon="Lock"
        />
      </el-form-item>

      <el-button
          type="primary"
          @click="handleSubmit"
          :loading="submitting"
          class="submit-btn"
          round
      >
        <el-icon v-if="!submitting" class="el-icon--left">
          <Refresh/>
        </el-icon>
        {{ submitting ? '正在提交...' : '立即修改' }}
      </el-button>
    </el-form>
  </el-card>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {ElMessage} from 'element-plus'
import {Lock, Refresh} from '@element-plus/icons-vue'
import PasswordStrength from '../../components/PasswordStrength.vue'
import {updateAdminPassword} from "../../service/userApi.js";
import {useStore} from "vuex";
import router from "../../router/index.js";
const store = useStore();
const formRef = ref()
const submitting = ref(false)
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const rules = {
  oldPassword: [
    {required: true, message: '请输入原密码', trigger: ['blur', 'change']}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: ['blur', 'change']},
    {
      pattern: /^(?=.*[A-Za-z])(?=.*\d).{6,}$/,
      message: '需至少6位且包含字母数字',
      trigger: ['blur', 'change']
    }
  ],
  confirmPassword: [
    {required: true, message: '请确认密码', trigger: ['blur', 'change']},
    {
      validator: (_, value, callback) => {
        if (value !== form.newPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    submitting.value = true
    const params = {
      oldPassword: form.oldPassword,
      newPassword: form.newPassword,
      confirmPassword: form.confirmPassword
    }
    console.log(params)
    const result = await updateAdminPassword(params)
    ElMessage.success({
      message: '密码修改成功，请重新登录',
      duration: 3000
    })
    // 再清前端
    await store.dispatch('logout')
    // 最后进行跳转
    await router.replace({path: '/login', query: {redirect: route.fullPath}})
    ElMessage.success('请重新登录')
  } finally {
    submitting.value = false
  }
}
</script>

<style lang="scss" scoped>
.password-card {
  max-width: 600px;
  margin: 2rem auto;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

  .title {
    text-align: center;
    margin-bottom: 2rem;
    color: #2c3e50;
    font-size: 1.5rem;
  }

  .el-form-item {
    margin-bottom: 1.5rem;

    :deep(.el-input__inner) {
      border-radius: 8px;
      padding: 12px 15px;
    }
  }

  .submit-btn {
    width: 100%;
    margin-top: 1.5rem;
    padding: 15px;
    font-size: 1rem;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
  }
}
</style>
